package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.OptHisVO;
import com.bbzn.device.client.vo.req.RangeEntity;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.MongoDbConstant;
import com.bbzn.device.client.dataobject.OptHis;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.SimpleMongoService;
import com.bbzn.device.client.utils.BaseApiService;
import com.bbzn.device.client.utils.BaseResponse;
import com.bbzn.device.client.utils.OptionalBean;
import com.bbzn.device.client.utils.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 黄志鹏
 * @Date 2021/3/16 17:11
 * @Version 1.0
 */

@RestController
@RequestMapping("/optHis")
@Slf4j
@Api(value = "optHis", tags = "健康管理")
@ApiSort(value = 1)// 大分类顺序
//@CrossOrigin
public class OptHisController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private SimpleMongoService simpleMongoService;

    /**
     * 查询日志数据列表
     *
     * @param reqVO
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getOptHisList", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询日志数据列表")
    public BaseResponse getOptHisList(@RequestBody OptHisVO reqVO, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long userId = commonService.getUserId(request);

        Map<String, Object> conditions = new HashMap<>();
        conditions.put("userId", userId);

        if(StringUtils.isNotEmpty(reqVO.getImei())){
            conditions.put("imei", reqVO.getImei());
        }

        Map<String, RangeEntity> map = new HashMap<>();
        if (OptionalBean.ofNullable(String.valueOf(reqVO.getStartTime())).isPresent()&& OptionalBean.ofNullable(String.valueOf(reqVO.getEndTime())).isPresent()) {
            RangeEntity rangeEntity = new RangeEntity();
            rangeEntity.setGte(reqVO.getStartTime());
            rangeEntity.setLt(reqVO.getEndTime());
            map.put("createTime", rangeEntity);
        }

        if (reqVO.getPageNum() == null) {
            reqVO.setPageNum(1);
        }
        if (reqVO.getPageSize() == null) {
            reqVO.setPageSize(50);
        }
        PageInfo<OptHis> page = simpleMongoService.selectPageByCondition(MongoDbConstant.OPT_HIS, map, conditions,null, OptHis.class, reqVO.getPageNum(), reqVO.getPageSize());
        return setResultSuccess(page);
    }


}
